package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Articles;
import com.ruoyi.system.service.IArticlesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新闻Controller
 *
 * @author AousVolad
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/articles/articles")
public class ArticlesController extends BaseController {

    @Autowired
    private IArticlesService articlesService;

    /**
     * 查询新闻列表
     */
    @PreAuthorize("@ss.hasPermi('articles:articles:list')")
    @GetMapping("/list")
    public TableDataInfo list(Articles articles, @Param("columnId") Long columnId) {
        articles.setColumnId(columnId);
        startPage();
        List<Articles> list = articlesService.selectArticlesList(articles);
        return getDataTable(list);
    }

    /*
       文章浏览量统计
     */
    @PreAuthorize("@ss.hasPermi('articles:articles:list')")
    @GetMapping("/dataAnalysis")
    public TableDataInfo dataAnalysis() {
        List<Articles> list = articlesService.getViewsData();
        return getDataTable(list);
    }


    /**
     * 导出新闻列表
     */
    @PreAuthorize("@ss.hasPermi('articles:articles:export')")
    @Log(title = "新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Articles articles) {
        List<Articles> list = articlesService.selectArticlesList(articles);
        ExcelUtil<Articles> util = new ExcelUtil<Articles>(Articles.class);
        util.exportExcel(response, list, "新闻数据");
    }

    /**
     * 获取新闻详细信息
     */
    @PreAuthorize("@ss.hasPermi('articles:articles:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(articlesService.selectArticlesById(id));
    }

    /**
     * 新增新闻
     */
    @PreAuthorize("@ss.hasPermi('articles:articles:add')")
    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Articles articles) {
        return toAjax(articlesService.insertArticles(articles));
    }

    /**
     * 修改新闻
     */
    @PreAuthorize("@ss.hasPermi('articles:articles:edit')")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Articles articles) {
        return toAjax(articlesService.updateArticles(articles));
    }

    /**
     * 删除新闻
     */
    @PreAuthorize("@ss.hasPermi('articles:articles:remove')")
    @Log(title = "新闻", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(articlesService.deleteArticlesByIds(ids));
    }
}
